package com.ojl.xmlparser;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XmlParse {
	
	public Document doc = null;
	

	public XmlParse(String xmlFile)
	{
		
		if (xmlFile.isEmpty())
			xmlFile = "raw\\file.xml";
		
		File fXmlFile = new File(xmlFile);
		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder = null;
		try {
			dBuilder = dbFactory.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			doc = dBuilder.parse(fXmlFile);
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		doc.getDocumentElement().normalize();
	}

	public String getAttribute(Element element, String attr)
	{
		return element.getAttribute(attr);
	}

	public ArrayList<String> getAttributes(Element element)
	{
		ArrayList<String> attributes = new ArrayList<String>();
		NamedNodeMap attNodes = element.getAttributes();
		for (int i=0; i<attNodes.getLength(); i++)
		{
			attributes.add(attNodes.item(i).getNodeName());
		}
			return attributes;
		
	}
	
	public String getAttribute(Object element, String attr)
	{
		Element ele = (Element) element;
		return ele.getAttribute(attr);
	}
	
	public ArrayList<String> getAttributes(Object element)
	{
		ArrayList<String> attributes = new ArrayList<String>();
		Element ele = (Element) element;		
		NamedNodeMap attNodes = ele.getAttributes();
		for (int i=0; i<attNodes.getLength(); i++)
		{
			attributes.add(attNodes.item(i).getNodeName());
		}
			return attributes;
		
	}	
	
	public String getElementName(Element element)
	{
		return element.getNodeName();
	}
	
	public String getElementValue(Element element)
	{
		return element.getNodeValue();
	}
	
	public String getElementType(Element element)
	{
		if (element.getNodeType() == Node.ELEMENT_NODE)
			return "ELEMENT";
		else if (element.getNodeType() == Node.ATTRIBUTE_NODE)
			return "ATTRIBUTE";		
		else if (element.getNodeType() == Node.COMMENT_NODE)
			return "COMMENT";
		else if (element.getNodeType() == Node.DOCUMENT_NODE)
			return "DOCUMENT";
		else if (element.getNodeType() == Node.TEXT_NODE)
			return "TEXT";
		else if (element.getNodeType() == Node.NOTATION_NODE)
			return "NOTATION";
		else if (element.getNodeType() == Node.CDATA_SECTION_NODE)
			return "CDATA";
		else if (element.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE)
			return "DOCUMENT_FRAGMENT";
		else if (element.getNodeType() == Node.ENTITY_NODE)
			return "ENTITY";
		else if (element.getNodeType() == Node.ENTITY_REFERENCE_NODE)
			return "ENTITY_REFERENCE";		
		else if (element.getNodeType() == Node.DOCUMENT_TYPE_NODE)
			return "DOCUMENT_TYPE";		
		else if (element.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE)
			return "PROCESSING";
		
		return "UNKOWN";
	}	
	
	public Object getRoot()
	{
		return getRoot(false);
	}
	
	public Object getRoot(Boolean text)
	{
		if (text)
			return getElementName((Element) getRoot());
		else
			return doc.getDocumentElement();
	}
	

	public ArrayList<?> getElements()
	{
		return getElements("", false);
	}
	
	public ArrayList<?> getElements(String tag)
	{
		return getElements(tag, false);
	}	
	
	public ArrayList<?> getElements(String tag, Boolean text)
	{
		ArrayList<Object> elements = new ArrayList<Object>();
		NodeList nodes;
		if (tag.isEmpty())
			nodes = doc.getChildNodes();
		else
			nodes = doc.getElementsByTagName(tag);
		for (int temp = 0; temp < nodes.getLength(); temp++)
		{
			   Node nNode = nodes.item(temp);
			   if (nNode.getNodeType() == Node.ELEMENT_NODE) {
			      Element eElement = (Element) nNode;
			      if (text)
			    	  elements.add(eElement.getNodeName());
			      else
			    	  elements.add(eElement);
			   }
		}		
		return elements;
	}		
	
	public ArrayList<?> getElements(Element element)
	{
		return getElements(element, false);
	}	
		
	public ArrayList<?> getElements(Element element, Boolean text)
	{
		ArrayList<Object> elements = new ArrayList<Object>();
		NodeList nodes = element.getChildNodes();
		for (int temp = 0; temp < nodes.getLength(); temp++)
		{
			   Node nNode = nodes.item(temp);
			   if (nNode.getNodeType() == Node.ELEMENT_NODE) {
			      Element eElement = (Element) nNode;
			      if (text)
			    	  elements.add(eElement.getNodeName());
			      else
			    	  elements.add(eElement);
			   }
		}		
		return elements;
	}		


	public ArrayList<?> getElements(Element element, String searchTag)
	{
		return getElements(element, searchTag, false);
	}		
	
	public ArrayList<?> getElements(Element element, String searchTag, Boolean text)
	{
		ArrayList<Object> elements = new ArrayList<Object>();
		NodeList nodes = element.getElementsByTagName(searchTag);
		for (int temp = 0; temp < nodes.getLength(); temp++)
		{
			   Node nNode = nodes.item(temp);
			   if (nNode.getNodeType() == Node.ELEMENT_NODE) {
			      Element eElement = (Element) nNode;
			      if (text)
			    	  elements.add(eElement.getNodeName());
			      else
			    	  elements.add(eElement);
			   }
		}		
		return elements;
	}	
	
	
}
